
import React,{useState,useEffect} from 'react'
import utils from '@utils'

let defaultParams={
  pageSize : 25,
  currentPage :1,
  'page.pageSize': 25,
  'page.currentPage': 1,
}

export default (url,params={},initData=true)=>{
  // 接口列表数据 , 已经翻页数据
  let [listData,setListData] = useState([])
  let [spinning,setSpin] = useState(false)
  let [totalCount,setTotalCount] = useState(0)
  let [currentPage,setCurrentPage] = useState(1)
  let [pageSize,setPageSize] = useState(25)
  
  let [init,setInit] = useState(initData)
  useEffect(()=>{
    // if(init){
    //   getListData({
    //     'currentPage': currentPage,
    //     'pageSize': pageSize,
    //     ...params
    //   })
    // }
    setInit(true)
  },[])

  const refreshListData = (params)=>{
    if(init){
      getListData({
        'currentPage': currentPage,
        'pageSize': pageSize,
        ...params
      })
      setInit(true)
    }
  }
  // 请求接口数据
  const getListData=(datas)=>{
    utils.get(url,{
      showMsg:false,
      ...datas,
    }).then(res=>{
      if(res.errorCode==0){
        let result = Array.isArray(res.result)?{data:res.result,totalCount:res.result.length,totalPage:1}:res.result
        let {data=[],totalCount,totalPage} = result//res.result
        setListData([...data])
        setTotalCount(totalCount)
        setCurrentPage(datas.currentPage)
        setPageSize(datas.pageSize)
      }
      setSpin(false)
    })
  }
  return {
    listData,spinning,totalCount,pageSize,currentPage,refreshListData
  }
}